<?php

namespace App\User\Http\Controller\Wap;

use App\ApiBaseController;
use App\User\Models\Ship;
use Illuminate\Http\Request;
use App\User\Services\ShipService;
use Illuminate\Support\Facades\Validator;

class ShipController extends ApiBaseController
{
    /**
     * 表单验证
     * @param $data
     * @param $type
     * @return bool
     * @throws GoodsException
     */
    protected function validate($data, $ruleArr, $source = '')
    {
        $rule = [
            'id' => 'required|integer|min:1',
            'u_id' => 'required|integer|min:1',
            'a_id' => 'required|integer|min:1',
            'name' => ['required', 'regex:/^[\x{4e00}-\x{9fa5}]+[·•]?[\x{4e00}-\x{9fa5}]+$/u', 'between:2,8'],
            'address' => 'required|string|between:5,100',
            'district' => 'required|string|between:5,100',
            'mobile' => ['required', 'regex:/^((\(\d{2,3}\))|(\d{3}\-))?1(3|5|8|9)\d{9}$/'],
            'is_default' => 'required|integer|between:'.Ship::DEFAULT_OFF.','.Ship::DEFAULT_ON
        ];

        // 根据场景做限制
        switch ($source) {
            case 'getData':
                $rule['id'] = 'nullable|integer|min:1';
                $rule['is_default'] = 'nullable|integer|between:'.Ship::DEFAULT_OFF.','.Ship::DEFAULT_ON;
                break;
        }

        $message = [
            'id.required' => '请传入收货地址ID',
            'id.integer' => '收货地址ID必须为整数',
            'id.min' => '收货地址ID必须大于0',
            'u_id.required' => '请传入用户ID',
            'u_id.integer' => '用户ID必须为整数',
            'u_id.min' => '用户ID必须大于0',
            'a_id.required' => '请传入地区ID',
            'a_id.integer' => '地区ID必须为整数',
            'a_id.min' => '地区ID必须大于0',
            'name.required' => '请输入收货人姓名',
            'name.regex' => '收货人姓名格式异常',
            'name.between' => '收货人姓名请限制在2-8个字符内',
            'address.required' => '请输入收货人地址',
            'address.regex' => '收货人地址格式异常',
            'address.between' => '收货人地址请限制在5-100个字符内',
            'district.required' => '请选择收货人省市区',
            'district.regex' => '收货人省市区格式异常',
            'district.between' => '收货人省市区请限制在5-100个字符内',
            'mobile.required' => '请输入收货人通知手机号',
            'mobile.regex' => '收货人通知手机号格式异常',
            'is_default.required' => '请选择是否为默认收货地址',
            'is_default.integer' => '默认收货地址参数必须为整数',
            'is_default.between' => '默认收货地址参数异常'
        ];

        // 根据配置选择规则组成
        $rule = array_intersect_key($rule, array_flip($ruleArr));

        $validate = Validator::make($data, $rule, $message);
        if($validate->fails()){
            throw new \Exception($validate->errors()->first());
        }
        return true;
    }

    /**
     * User: zhangchaoqun
     * DateTime: 2021/6/15 1:18 下午
     * Describe: 添加收货地址
     * Version: V1.0
     * @param Request $request
     */
    public function addShip(Request $request)
    {
        // 接受参数
        $params = $request->input();
        // 参数字段
        $fields = ['a_id', 'address', 'name', 'mobile', 'district'];

        try {
            // 校验参数
            $this->validate($params, $fields);

            $shipService = new ShipService();
            $shipService->addData($params);
            return $this->success('添加成功');
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * User: zhangchaoqun
     * DateTime: 2021/6/15 2:27 下午
     * Describe: 编辑收货地址
     * Version: V1.0
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function editShip(Request $request)
    {
        // 接受参数
        $params = $request->input();
        // 参数字段
        $fields = ['id', 'a_id', 'address', 'name', 'mobile', 'district'];

        try {
            // 校验参数
            $this->validate($params, $fields);

            $shipService = new ShipService();
            $shipService->editData($params);
            return $this->success('保存成功');
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * User: zhangchaoqun
     * DateTime: 2021/6/15 2:08 下午
     * Describe: 设置默认收货地址
     * Version: V1.0
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function setDefault(Request $request)
    {
        // 接受参数
        $params = $request->input();
        // 参数字段
        $fields = ['id', 'is_default'];

        try {
            // 校验参数
            $this->validate($params, $fields);

            $shipService = new ShipService();
            $shipService->setDefault($params);
            return $this->success('设置成功');
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * User: zhangchaoqun
     * DateTime: 2021/6/15 1:28 下午
     * Describe: 删除收货地址
     * Version: V1.0
     * @param Request $request
     */
    public function delShip(Request $request)
    {
        // 接受参数
        $params = $request->input();
        // 参数字段
        $fields = ['id'];

        try {
            // 校验参数
            $this->validate($params, $fields);

            $shipService = new ShipService();
            $shipService->delData($params);
            return $this->success('删除成功');
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * User: zhangchaoqun
     * DateTime: 2021/6/15 1:40 下午
     * Describe: 获取收货地址列表
     * Version: V1.0
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getList(Request $request)
    {
        // 接受参数
        $params = $request->input();

        $limit = $request->input('limit', 10);
        try {

            $shipService = new ShipService();
            $data = $shipService->getList($params, $limit);
            return $this->success('获取成功', $data);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * User: zhangchaoqun
     * DateTime: 2021/6/15 1:56 下午
     * Describe: 获取收货地址详情
     * Version: V1.0
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getData(Request $request)
    {
        // 接受参数
        $params = $request->input();
        // 参数字段
        $fields = ['id', 'is_default'];

        try {
            // 校验参数
            $this->validate($params, $fields, 'getData');
            $shipService = new ShipService();
            $data = $shipService->getDetail($params);
            return $this->success('获取成功', $data);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }
}
